<%--
    Document   : index
    Created on : 19 juin 2010, 23:51:05
    Author     : marya
--%>

<%@page contentType="text/xml" pageEncoding="UTF-8"%>
<%@page import="org.restlet.resource.ClientResource" %>
<%@page import="org.restlet.representation.Representation" %>
<%@page import="org.restlet.ext.xml.DomRepresentation" %>
<%@page import="org.restlet.Response" %>
<%@page import="org.restlet.Request" %>
<%@page import="org.restlet.data.Method" %>
<%@page import="org.restlet.data.*" %>
<%@page import="org.w3c.dom.Element" %>
<%@page import="org.w3c.dom.Node" %>
<%@page import="org.w3c.dom.NodeList" %>
<%@page import="java.util.List" %>
<%@page import="java.util.Map" %>
<%@page import="xmlws.helpers.WebService" %>
<%
            int idUser = Integer.parseInt(request.getSession().getAttribute("idUser").toString());

            ClientResource client = new ClientResource(WebService.federateur + "/users/" + idUser + "/promotions");

            DomRepresentation representation = null;

            if (request.getMethod().equals("GET")) {
                //On récupère les  promotions de l'utilisateur

                representation = new DomRepresentation(client.get());
                NodeList elements = representation.getDocument().getElementsByTagName("promotion");
                if (elements.getLength() > 0) {
                    for (int i = 0; i < elements.getLength(); i++) {
                        Node node = elements.item(i);
                        String[] tab = node.getAttributes().
                                getNamedItem("href").getTextContent().split("/");

                        String id = tab[tab.length - 1];
                        out.println("<div class='promotion' onclick=\"get_promotion(" + id + ")\" id=\"promotion" + id +"\">");
                        out.println(node.getFirstChild().getTextContent());
                        out.println("</div><br/>");
                    }
                }

            } else if (request.getMethod().equals("POST")) {

                Form form = new Form();


                form.add("nomPromotion", request.getParameter("nomPromotion"));
                form.add("uriPromotion", request.getParameter("uriPromotion"));



                representation = new DomRepresentation(client.post(form.getWebRepresentation()));


                if (client.getResponse().getStatus().equals(Status.SUCCESS_CREATED)) {
                    response.setStatus(200);
                     //Si le status est OK, on affiche le nouveau contact en faisant un get
                    //Sur l'id de contact contenu dans la representation de reponse

                    Node element = representation.getDocument().getDocumentElement();
                    String[] tab = element.getAttributes().
                            getNamedItem("href").getTextContent().split("/");

                    String id = tab[tab.length - 1];
                    

                    client = new ClientResource(WebService.federateur + "/promotions/" + id);

                    representation = new DomRepresentation(client.get());
                    NodeList elements = representation.getDocument().getElementsByTagName("hcard");
                    //NodeList elements = representation.getNodes("//*[@class]"); marche pas
                    if (elements.getLength() > 0) {
                        for (int i = 0; i < elements.getLength(); i++) {
                            Node node = elements.item(i);
                            out.println(node.getTextContent());
                        }
                    }
                    

                }else{


                    response.setStatus(client.getResponse().getStatus().getCode());
                    out.println(client.getResponseEntity().getText());

               }


            }




%>

